package com.itcast.plane.modoule.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.plane.common.ConsHint;
import com.itcast.plane.modoule.user.domain.User;
import com.itcast.plane.modoule.user.dto.UserEditDTO;
import com.itcast.plane.modoule.user.dto.UserLoginDTO;
import com.itcast.plane.modoule.user.mapper.UserMapper;
import com.itcast.plane.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    @Resource
    private UserMapper userMapper;

    public JsonResult addUser(UserLoginDTO userDTO) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        lambdaQueryWrapper.eq(User::getPhone,userDTO.getPhone());
        User user2 = userMapper.selectOne(lambdaQueryWrapper);
        if (ObjectUtils.isEmpty(user2)) {
            lambdaQueryWrapper = new LambdaQueryWrapper<>();
            User user1 = userMapper.selectOne(lambdaQueryWrapper.eq(User::getAccount, user.getAccount()));
            if (ObjectUtils.isEmpty(user1)) {
                userMapper.insert(user);
                return new JsonResult(true, ConsHint.INSERT_SUCCESS_LOGIN);
            } else {
                return new JsonResult(false, ConsHint.INSERT_DEFAULTED_LOGIN);
            }
        }else {
            if ("".equals(user2.getAccount()) || null == user2.getAccount()){
                BeanUtils.copyProperties(userDTO,user2);
                userMapper.updateById(user2);
                return new JsonResult(true, ConsHint.INSERT_SUCCESS_LOGIN);
            }else {
                return new JsonResult(false, ConsHint.INSERT_DEFAULTED_LOGIN);
            }
        }
    }

    public JsonResult loginUser(UserLoginDTO userDTO) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        User user = userMapper.selectOne(lambdaQueryWrapper.eq(User::getAccount,userDTO.getAccount()));
        if (!ObjectUtils.isEmpty(user)) {
            if (user.getPassword().equals(userDTO.getPassword())) {
                return new JsonResult(true, ConsHint.LOGIN_SUCCESS,user);
            }
        }
        return new JsonResult(false,ConsHint.LOGIN_DEFAULT);
    }

    public JsonResult editUser(UserEditDTO editDTO) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        User user = userMapper.selectOne(lambdaQueryWrapper.eq(User::getPhone,editDTO.getPhone()));
        if (ObjectUtils.isEmpty(user)) {
            User user1 = new User();
            BeanUtils.copyProperties(editDTO,user1);
            userMapper.updateById(user1);
        }
        return new JsonResult(false,"手机号已存在");
    }
}
